package com.itheima.service;



import com.itheima.dao.TeacherDao;
import com.itheima.domain.Teacher;

public class TeacherService {
    private TeacherDao teacherDao = new TeacherDao();

    /**
     * @param teacher
     * @return 返回flag如果为true表示添加成功, false则表示添加失败
     */
    //添加学生的方法
    public boolean addTeacher(Teacher teacher) {
        boolean flag = teacherDao.addTeacher(teacher);
        return flag;
    }

    //删除学生的方法
    public void deleteTeacher(String sid) {
        teacherDao.deleteTeacher(sid);
    }

    //修改学生的方法
    public void updateTeacher(String sid,Teacher teacher) {
        teacherDao.updateTeacher(sid,teacher);
    }

    //查看学生信息的方法
    public  Teacher[] showTeacher() {

        Teacher[] teacher = teacherDao.showTeacher();
        //判断teacher是否为空数组
        int index = -1;
        for (int i = 0; i < teacher.length; i++) {
            if (teacher[i] != null){
                index = i;
            }
        }
        if (index == -1){
            teacher = null;
        }
        return teacher;
    }

    //判断学生学号是否存在的方法
    public boolean isExists(String sid) {
        //可以找到数组里与sid相同的数据返回true
        Teacher[] teachers = teacherDao.findAllTeacher();
        boolean flag = false;
        for (int i = 0; i < teachers.length; i++) {
            if (teachers[i] != null && sid.equals(teachers[i].getSid())) {
                flag = true;
                break;
            } else {
                flag = false;
            }
        }
        return flag;
    }

    public boolean isNull(String flag){
        if (flag.equals("")){
            return false;
        }
        return true;
    }
}
